<template>
	<view>
		<nav-bar title="我的收藏"></nav-bar>
        <!-- 公共组件-每个页面必须引入 -->
        <public-module></public-module>
		<view class="collection_classification">
			<view :class="{ active: select == 1000 }" @click="onSelect(1000)">商品</view>
			<view :class="{ active: select == 1001 }" @click="onSelect(1001)">店铺</view>
		</view>
        <mescroll-body ref="mescrollRef" height="-98" @init="mescrollInit" :down="downOption" @down="downCallback" @up="upCallback">
            <view v-if="select == 1000">
                <view class="collection_product_box" v-for="(item,index) of goodsList" :key="item.objId" @click="onPageJump(1000,item)">
                    <image :src="item.img" mode="aspectFill"></image>
                    <view class="product_info">
                        <view class="name">
                            <text>{{item.name}}</text>
                            <view>销量：{{item.numSales}}</view>
                        </view>
                        <view class="price">
                            <text>{{item.priceDiscount}}</text>
                        </view>
                    </view>
                    <view class="collection_status" @click.stop="onDelete(index,1402)">
                        <view></view>
                        <text>取消收藏</text>
                    </view>
                </view>
            </view>
            <view class="bg_box_container" v-if="select == 1001">
                <view class="collection_shop_box" v-for="(item,index) of shopList" :key="item.objId">
                    <view class="shop_info">
                        <image :src="item.logo" mode="aspectFill"></image>
                        <view class="shop_details">
                            <text @click="onPageJump(1001,item)">{{item.name}}</text>
                            <!-- <view> -->
                                <!-- <view>自营</view> -->
                                <!-- <text>54.3万人关注</text> -->
                            <!-- </view> -->
                        </view>
                        <view class="shopcollection_status" @click="onDelete(index,1401)">
                            <view></view>
                            <text>取消收藏</text>
                        </view>
                    </view>
                    <!-- <view class="shop_productimg">
                        <image src="../../static/images/1.jpg" mode="aspectFill"></image>
                        <image src="../../static/images/1.jpg" mode="aspectFill"></image>
                        <image src="../../static/images/1.jpg" mode="aspectFill"></image>
                    </view> -->
                </view>
            </view>
        </mescroll-body>
	</view>
</template>

<script>
	import MescrollMixin from "@/components/common/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		data() {
			return {
				dataInfo: {},
				select: 1000,
				shopCollection: true,
				productCollection: true,
				downOption: {
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				goodsList: [],
				shopList: [],
			};
		},
		//第一次加载
		onLoad(e) {
		},
		//页面显示
		onShow() {},
		//方法
		methods: {
			onPageJump(type, item) {
				let url = ""
				if (type == 1000) {
					url = '/pages/home/shop/goodsDetail?objId=' + item.objId
				} else {
					url = '/pages/home/shop/shop?objId=' + item.objId
				}
				uni.navigateTo({
					url: url
				})
			},
			onSelect(type) {
				this.select = type;
				this.mescroll.triggerDownScroll();
			},
			// 取消收藏
			onDelete(index, type) {
				let item;
				if (type == 1402) {
					item = this.goodsList[index];
				} else {
					item = this.shopList[index];
				}
				this.$http.post('api/collect/v1/operate_data', {
					objIds: [item.objId],
					type: type
				}).then(res => {
					if (type == 1402) {
						this.goodsList.splice(index, 1);
					} else {
						this.shopList.splice(index, 1);
					}
					uni.showToast({
						title: "取消收藏成功",
					});
				});
			},
            /*下拉刷新的回调 */
            downCallback() {
            	//联网加载数据
            	this.loadData(1);
            },
            /*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
            upCallback(page) {
            	//联网加载数据
            	this.loadData(page.num);
            },
            loadData(pageNo) {
                let httpData = {
                    pageNo: pageNo,
                    pageSize: 15
                };
                let url = "";
                if (this.select == 1000) {
                	url = "api/collect/v1/mall_goods_list";
                } else if (this.select == 1001) {
                	url = "api/collect/v1/list_data";
                	httpData = {
                		pageSize: 15,
                		pageNo: pageNo,
                		shopType: 1401
                	}
                }
                this.$http
                	.post(url, httpData,{
                		load:false
                	}).then(res => {
                		uni.stopPullDownRefresh();
                		//方法一(推荐): 后台接口有返回列表的总页数 totalPage
                		this.mescroll.endByPage(res.data.length, res.pages); //必传参数(当前页的数据个数, 总页数)
                		if (this.select == 1000) {
                            if (pageNo == 1) {
                            	this.goodsList = res.data;
                            } else {
                            	this.goodsList = this.goodsList.concat(res.data);
                            }
                        } else if(this.select == 1001){
                            if (pageNo == 1) {
                            	this.shopList = res.data;
                            } else {
                            	this.shopList = this.shopList.concat(res.data);
                            }
                        }
                	}).catch(() => {
                		//联网失败, 结束加载
                		this.mescroll.endErr();
                	});
            },
		},
		//页面隐藏
		onHide() {},
		//页面卸载
		onUnload() {},
		//用户点击分享
		onShareAppMessage(e) {
			return this.wxShare();
		}
	};
</script>
<style lang="scss" scoped>
	@import '@/style/mixin.scss';

	.collection_classification {
		padding: 14rpx 68rpx;
		background: #ffffff;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.active {
			border-radius: 30rpx;
			color: #ffffff;
			@include theme('gradient_bg');
		}

		>view {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 240rpx;
			height: 60rpx;
			font-size: 28rpx;
			color: #333333;
		}
	}

	.collection_product_box {
		margin: 20rpx 30rpx 0rpx 30rpx;
		padding: 20rpx 15rpx 20rpx 20rpx;
		background: #ffffff;
		display: flex;
		justify-content: space-between;
		align-items: center;

		>image {
			width: 160rpx;
			height: 160rpx;
			border-radius: 8rpx;
		}

		.product_info {
			flex: 1;
			display: flex;
			flex-direction: column;
			padding-left: 30rpx;
			border-radius: 8rpx;

			.name {
				font-size: 28rpx;
				color: #333435;

				>view {
					color: #cccccc;
					font-size: 24rpx;
					padding-top: 10rpx;
				}
			}

			.price {
				font-size: 24rpx;
				color: #ff4040;
				padding-top: 34rpx;

				>text {
					font-size: 28rpx;
				}
			}
		}

		.collection_status {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			width: 96rpx;
			margin: 10rpx;
			height: 82rpx;

			>view {
				width: 38rpx;
				height: 38rpx;
				background-size: 100% 100%;
				background-position: center center;
				background-repeat: no-repeat;
				background-image: url('../../static/icon/theme/ic_collection.png');
			}

			>text {
				margin-top: 11rpx;
				font-size: 24rpx;
				color: #333333;
			}
		}
	}

	.bg_box_container {
		width: 100%;
		background: #ffffff;

		.collection_shop_box {
			display: flex;
			flex-direction: column;
			background: #ffffff;
			padding: 30rpx 30rpx 30rpx 30rpx;

			.shop_info {
				display: flex;
				justify-content: space-between;
				align-items: center;

				>image {
					display: flex;
					justify-content: center;
					width: 80rpx;
					height: 80rpx;
				}

				.shop_details {
					display: flex;
					flex: 1;
					padding-left: 30rpx;
                    align-items: center;
					>text {
						font-size: 32rpx;
						color: #333333;
						// padding-bottom: 15rpx;
						position: relative;
                        max-width: 360rpx;
                        @include toe();
					}
                    &:after {
                    	content: '';
                    	background-image: url('../../static/icon/theme/ic_shop_to.png');
                    	background-repeat: no-repeat;
                    	background-size: 100% 100%;
                    	background-position: center center;
                    	width: 30rpx;
                    	height: 30rpx;
                    }
					// >view {
					// 	display: flex;
					// 	justify-content: flex-start;
					// 	align-items: center;

					// 	>view {
					// 		font-size: 24rpx;
					// 		color: #ffffff;
					// 		padding: 2rpx 20rpx;
					// 		@include theme('gradient_bg');
					// 		border-radius: 15rpx;
					// 	}

					// 	>text {
					// 		font-size: 24rpx;
					// 		color: #999999;
					// 		padding-left: 12rpx;
					// 	}
					// }
				}

				.shopcollection_status {
					display: flex;
					flex-direction: column;
					align-items: center;
					width: 96rpx;
					height: 82rpx;

					>view {
						width: 38rpx;
						height: 38rpx;
						background-size: 100% 100%;
						background-position: center center;
						background-repeat: no-repeat;
						background-image: url('../../static/icon/theme/ic_collection.png');
					}

					>text {
						margin-top: 11rpx;
						font-size: 24rpx;
						color: #333333;
					}
				}
			}

			.shop_productimg {
				display: flex;
				justify-content: space-between;
				margin-left: 107rpx;
				padding: 30rpx 0rpx;
				border-bottom: 1rpx solid #e5e5e5;

				>image {
					width: 186rpx;
					height: 186rpx;
					border-radius: 8rpx;
					margin: 0rpx 5rpx;
				}
			}
		}
	}
</style>
